define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'productslist/index' + location.search + '&ids=' + Config.cid,
                    add_url: 'productslist/add?ids=' + Config.cid,
                    edit_url: 'productslist/edit',
                    del_url: 'productslist/del',
                    multi_url: 'productslist/multi',
                    import_url: 'productslist/import',
                    table: 'productslist',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        //{field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'from', title: __('From'), operate: 'LIKE'},
                        //{field: 'keywords', title: __('Keywords'), operate: 'LIKE'},
                        //{field: 'description', title: __('Description'), operate: 'LIKE'},
                        {field: 'tuiswitch', title: __('Tuiswitch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'shswitch', title: __('Shswitch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'productstype', title: __('Productstype')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"),function(data, ret) {
                    console.log(data);
                    // 表单提交成功后的回调
                    if (data.url) {
                        // 如果保存成功，跳转到返回的预览页面URL
                        window.open(data.url, '_blank');
                    }
                }, function(data, ret) {
                    // 表单提交失败后的回调
                    alert(ret.msg);
                });

                var specificationIndex = initialSpecificationIndex;
                $('#add-specification').click(function () {
                    var specificationItem = '<div class="specification-item form-inline mb-2">' +
                        '<input class="form-control" name="specifications[' + specificationIndex + '][spec_name]" type="text" placeholder="尺寸" style="width: 30%;">' +
                        '<input class="form-control" name="specifications[' + specificationIndex + '][spec_name2]" type="text" placeholder="颜色" style="width: 30%;">' +
                        '<input class="form-control" name="specifications[' + specificationIndex + '][spec_price]" type="number" placeholder="价格" style="width: 30%;">' +
                        '<input class="form-control" name="specifications[' + specificationIndex + '][spec_name_en]" type="text" placeholder="尺寸-英文" style="width: 30%;">' +
                        '<input class="form-control" name="specifications[' + specificationIndex + '][spec_name2_en]" type="text" placeholder="颜色-英文" style="width: 30%;">' +
                        '<input class="form-control" name="specifications[' + specificationIndex + '][url]" type="text" placeholder="跳转地址" style="width: 85%;">' +

                        '<div class="col-xs-12 col-sm-12">\n' +
                        '                        <div class="input-group">\n' +
                        '                            <input id="c-image-spec-' + specificationIndex + '" class="form-control" size="50" name="specifications[' + specificationIndex + '][spec_image]" type="text" value="">\n' +
                        '                            <div class="input-group-addon no-border no-padding">\n' +
                        '                                <span><button type="button" id="faupload-image-spec-' + specificationIndex + '" class="btn btn-danger faupload" data-input-id="c-image-spec-' + specificationIndex + '" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp,image/webp" data-multiple="false" data-preview-id="p-image-spec-' + specificationIndex + '"><i class="fa fa-upload"></i>上传</button></span>\n' +
                        '                                <span><button type="button" id="fachoose-image-spec-' + specificationIndex + '" class="btn btn-primary fachoose" data-input-id="c-image-spec-' + specificationIndex + '" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i>选择</button></span>\n' +
                        '                            </div>\n' +
                        '                            <span class="msg-box n-right" for="c-image-spec-' + specificationIndex + '"></span>\n' +
                        '                        </div>\n' +
                        '                        <ul class="row list-inline faupload-preview" id="p-image-spec-' + specificationIndex + '"></ul>\n' +
                        '                    </div>' +

                        '<button type="button" class="btn btn-danger remove-specification">删除</button>' +
                        '</div>';
                    $('#specifications-container').append(specificationItem);

                    // 初始化 faupload
                    Form.events.plupload($("form"));

                    specificationIndex++;
                });

                $(document).on('click', '.remove-specification', function () {
                    $(this).parent().remove();
                });

                $(document).on('click', '.btn-publish', function () {
                    var options = {url: 'productslist/multi', data: {action: '', ids: $(this).data("id"), params: 'shswitch=1'}};
                    Fast.api.ajax(options, function (){
                        parent.layer.closeAll();
                        // 刷新父页面中的表格
                        parent.$("#table").bootstrapTable('refresh');
                    });
                });
            }
        }
    };
    return Controller;
});
